package com.mobisys.live.controller.wechat;

import com.mobisys.live.bean.channel.GetAllLiveList;
import com.mobisys.live.bean.im_group.NewGroupBean;
import com.mobisys.live.bean.live.CreateNewLive;
import com.mobisys.live.bean.live.GetLiveInfo;
import com.mobisys.live.bean.live.Guest;
import com.mobisys.live.bean.live.Schedule;
import com.mobisys.live.dao.mapper.*;
import com.mobisys.live.dao.model.*;
import com.mobisys.live.im.TimRestApi;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.channel.LiveResponseBody;
import com.mobisys.live.response.live.LiveInfoResponseBody;
import com.mobisys.live.response.live.SimpleLiveInfoResponseBody;
import com.mobisys.live.response.live.WechatLiveResponseBody;
import com.mobisys.live.response.live.WechatSimpleLiveResponseBody;
import com.mobisys.live.response.tim.CreateGroupResponse;
import com.mobisys.live.util.LiveUtil;
import com.mobisys.live.util.PictureUtil;
import com.mobisys.live.util.UUIDUtil;
import com.mobisys.live.util.ValidationUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * Created by zhuo on 2018-01-16.
 * 微信小程序直播接口
 */
@RestController
public class WechatLiveController {
    private Logger logger= LogManager.getLogger(WechatLiveController.class.getName());

    @Autowired
    private LiveMapper liveMapper;

    @Autowired
    private UserMapper userMapper;




    /**
     * 获取微信小程序的三张头图
     * @return
     */
    @RequestMapping(value = "/0.1/wechat/top_picture",method = RequestMethod.GET)
    public Response getWechatTopPicture(){
        Response response=new Response();
        return response;
    }

    /**
     * 获取直播列表
     * @param page
     * @param size
     * @param type
     * @param sort

     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/0.1/wechat/live",method = RequestMethod.GET)
    public Response getLiveList(@RequestParam int page,@RequestParam int size,@RequestParam String type,
                                @RequestParam String sort, HttpServletRequest httpServletRequest){

        Response response=new Response<List<WechatSimpleLiveResponseBody>>();



        int pageOffset=(page-1)*10;
        if(type.equals("0")){
            type=null;
        }else if(type.equals("1")){
            type="学术";
        }else if(type.equals("2")){
            type="晚会";
        }else {
            type=null;
        }
        List<LiveWithBLOBs> list=liveMapper.selectByPage(pageOffset,10,type,null);
        if(list==null){
            response.setRetureValue(LIST_IS_EMPTY);
            response.setRetureMsg(MSG_LIST_IS_EMPTY);
            return response;
        }
        List<WechatSimpleLiveResponseBody> liveResponseBodies=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            LiveWithBLOBs liveWithBLOBs=list.get(i);
            WechatSimpleLiveResponseBody responseBody=new WechatSimpleLiveResponseBody();
            responseBody.setLiveId(liveWithBLOBs.getId());
            responseBody.setUserId(liveWithBLOBs.getUserId());
            responseBody.setLiveName(liveWithBLOBs.getTitle());

            //获取用户名
            User user=userMapper.selectByPrimaryKey(liveWithBLOBs.getUserId());
            responseBody.setUserName(user.getNickName());

            responseBody.setViewCount(liveWithBLOBs.getViewCount());
            responseBody.setLikeCount(liveWithBLOBs.getLikeCount());

            //String avatorUrl=PictureUtil.getPictureUrlFromId(user.getHeadPicId());
            //String coverUrl=PictureUtil.getPictureUrlFromId(liveWithBLOBs.getCoverPictureId());

            String avatorUrl="";
            String coverUrl="";

            responseBody.setAvatorUrl(avatorUrl);
            responseBody.setCoverPictureUrl(coverUrl);

            responseBody.setLocation(liveWithBLOBs.getLocation());
            responseBody.setType(liveWithBLOBs.getType());

            responseBody.setLiveStatus(liveWithBLOBs.getLiveStatus());

            liveResponseBodies.add(responseBody);
        }
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(liveResponseBodies);

        return response;


    }


    /**
     * 获取直播详情信息
     * @param liveId

     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "/0.1/wechat/live/{liveId}",method = RequestMethod.GET)
    public Response getVideoDetail(@PathVariable String liveId, HttpServletRequest httpServletRequest) {
        Response response=new Response();


        LiveWithBLOBs liveWithBLOBs=liveMapper.selectByPrimaryKey(liveId);
        if(liveWithBLOBs==null){
            response.setRetureValue(LIST_IS_EMPTY);
            response.setRetureMsg(MSG_LIST_IS_EMPTY);
            return response;
        }

        WechatLiveResponseBody responseBody=new WechatLiveResponseBody();
        responseBody.setLiveId(liveWithBLOBs.getId());
        responseBody.setUserId(liveWithBLOBs.getUserId());
        responseBody.setChannelId(liveWithBLOBs.getChannelid());
        responseBody.setGroupId(liveWithBLOBs.getGroupid());

        User user=userMapper.selectByPrimaryKey(liveWithBLOBs.getUserId());
        String avatorUrl="";//PictureUtil.getPictureUrlFromId(user.getHeadPicId());
        String coverUrl="";//PictureUtil.getPictureUrlFromId(liveWithBLOBs.getCoverPictureId());

        responseBody.setAvatorUrl(avatorUrl);
        responseBody.setCoverPictureUrl(coverUrl);
        responseBody.setPlayUrl(liveWithBLOBs.getFlvPlayUrl());//
        responseBody.setRtmpPlayUrl(liveWithBLOBs.getRtmpPlayUrl());//
        responseBody.setLiveName(liveWithBLOBs.getTitle());
        responseBody.setUserName(user.getNickName());
        responseBody.setStartTime(liveWithBLOBs.getCreateTime().getTime()+"");
        responseBody.setEndTime(liveWithBLOBs.getEndTime().getTime()+"");
        responseBody.setViewCount(liveWithBLOBs.getViewCount());
        responseBody.setLikeCount(liveWithBLOBs.getLikeCount());
        responseBody.setLocation(liveWithBLOBs.getLocation());
        responseBody.setIntroduction("");
        responseBody.setType(liveWithBLOBs.getType());

        responseBody.setLiveStatus(liveWithBLOBs.getLiveStatus());

        //获取嘉宾列表
        responseBody.setGuestList(null);

        //获取议程列表
        responseBody.setAgendaList(null);

        response.setRetureData(responseBody);
        response.setRetureMsg(MSG_SUCCESS);

        return response;
    }

}
